import { createStore } from 'vuex'
import { getUserInfo } from '@/http/request/user'

export default createStore({
  state: {
    isLogin: false,
    userInfo: undefined
  },
  mutations: {
    setLogin (state, data) {
      state.isLogin = data
    },
    setUserInfo (state, data) {
      state.userInfo = data
    }
  },
  actions: {
    async initUserInfo ({ commit, state }) {
      if (state.isLogin) {
        return true
      }
      const res = await getUserInfo().then((res) => {
        return res.data
      }).catch(() => {
        return false
      })
      if ((res && res.errno !== 0) || !res) {
        return false
      }
      commit('setLogin', true)
      commit('setUserInfo', res.data)
      return true
    }
  },
  modules: {
  }
})
